package org.xxpay.pay.channel.zhuming;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.HtmlUtils;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.common.util.sign.encrypt.MD5;
import org.xxpay.common.util.sign.encrypt.RC4;
import org.xxpay.common.util.str.StringUtils;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.AmountUtil;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;
import org.xxpay.pay.channel.maicheng.MaichengConfig;
import org.xxpay.pay.channel.zhuming.util.ZhumingSign;

import java.util.ArrayList;
import java.util.Map;

@Component
public class ZhumingPaymentService extends BasePayment {
    private final static String logPrefix = "【著茗支付】";

    // 下单
    private final static String ORDER_CREATE_STR = "/gateway/index/checkpoint.do";
    @Override
    public String getChannelName() {
        return ZhumingConfig.CHANNEL_NAME;
    }
    /**
     * 支付下单
     * @param payOrder
     * @return
     */
    @Override
    public JSONObject pay(PayOrder payOrder){
        return doOrderReq(payOrder);
    }

    /**
     * 查询订单
     * @param payOrder
     * @return
     */
    @Override
    public JSONObject query(PayOrder payOrder){
        JSONObject retObj = buildRetObj();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
        retObj.put(PayConstant.RESULT_PARAM_ERRDES, "没有订单查询接口");
        return retObj;
    }

    //下单具体实现
    private JSONObject doOrderReq(PayOrder payOrder){
        ZhumingConfig config = new ZhumingConfig(getPayParam(payOrder));
        Map<String,Object> params = Maps.newHashMap();
        //设置请求参数
        setParams(payOrder, config, params);
        _log.info("{}请求数据:{}", logPrefix, JSON.toJSONString(params));
        //将订单改为支付中
        rpcCommonService.rpcPayOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
        String payForm = HtmlUtils.form(config.getRequestUrl(), HtmlUtils.POST, params);
        JSONObject retObj = new JSONObject();
        JSONObject payInfo = new JSONObject();
        payInfo.put("payUrl",payForm);
        payInfo.put("payMethod",PayConstant.PAY_METHOD_FORM_JUMP);
        retObj.put("payParams", payInfo);
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_SUCCESS);
        return retObj;
    }

    //设置请求参数
    private void setParams(PayOrder payOrder, ZhumingConfig config, Map<String,Object> params){
        params.put("account_id", config.getMchId());
        params.put("content_type", "text");
        params.put("thoroughfare", config.getPayMode());
        params.put("atype", config.getPayMode());
        params.put("out_trade_no", payOrder.getPayOrderId());
        params.put("robin", "2");
        params.put("keyId", config.getPrivateKey());
        params.put("amount", AmountUtil.convertCent2Dollar(String.valueOf(payOrder.getAmount())));
        params.put("callback_url", payConfig.getNotifyUrl(getChannelName()));
        params.put("success_url", "https://www.baidu.com/");
        params.put("error_url", "https://www.baidu.com/");
        String sign = params.get("amount").toString() + params.get("out_trade_no");
        String first = MD5.sign(sign,"",SignUtils.CHARSET_UTF8);
        byte [] rc4 = RC4.encry_RC4_byte(first,config.getPrivateKey());
        String fmD5 = MD5.sign(rc4);
        params.put("sign", fmD5);
        _log.info("{}签名结果数据:{}", logPrefix, fmD5);
    }
}
